class City < ActiveRecord::Base
  has_many :universities
  has_many :advertisments , :through=> :universities

  def search_advertisments(searchTerm,category,location)
    subCategories=category.sub_categories
    searchTerm = (searchTerm.nil? ? "" : searchTerm)
    searchTerm = "%#{searchTerm.downcase}%"
    all_univ = self.universities
    Advertisment.find(:all, :conditions => ["university_id in (?) and (title like ? or description like ?) and (location like ? ) and sub_category_id in (?)",all_univ.collect{|x| "#{x.id}"}.join(","),searchTerm,searchTerm,location,subCategories.collect{|x| "#{x.id}"}.join(",")], :order => "created_at desc")
  end

   def search_advertisments_by_categories(searchTerm,category)
    subCategories=category.sub_categories
    searchTerm = (searchTerm.nil? ? "" : searchTerm)
    searchTerm = "%#{searchTerm.downcase}%"
    all_univ = self.universities
    Advertisment.find(:all, :conditions => ["university_id in (?) and (title like ? or description like ?) and sub_category_id in (?)",all_univ.collect{|x| "#{x.id}"}.join(","),searchTerm,searchTerm,subCategories.collect{|x| "#{x.id}"}.join(",")],:order => "created_at desc")
  end




end
